﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Possan.Localization.Scanner
{
	class FileWalker
	{
		CodeScanner scanner;

		public FileWalker(CodeScanner cs)
		{
			scanner = cs;
		}

		public void Walk(string filemask)
		{
			string dir = "";
			string mask = "*.*";

			if( Directory.Exists( filemask ) )
			{
				dir = filemask;
			}
			else
			{
				dir = Path.GetDirectoryName(filemask);
				mask = Path.GetFileName(filemask);
			}

			if (dir != "")
			{
				WalkDir(dir, mask, true, 0);
			}
		}

		void WalkDir(string dir, string mask, bool recursive, int depth)
		{
			// Console.WriteLine("Processing directory: " + dir + " (filemask " + mask + ")");

			string[] files = Directory.GetFiles(dir, mask);
			foreach( string f in files )
			{
				ScanFile(f);
			}

			if (recursive && depth<10)
			{
				string[] subdirs = Directory.GetDirectories(dir);
				foreach (string d in subdirs)
				{
					WalkDir(d, mask, recursive, depth+1);
				}
			}
		}

		void ScanFile(string path)
		{
			string ext = Path.GetExtension(path).ToLower();
			if (ext == ".cs")
			{
				// Console.WriteLine("Scanning file: " + path);
				scanner.ScanCsharpFile(path);
			}
//			if (ext == ".vb")
//			{
//				Console.WriteLine("Scanning file: " + path);
//				scanner.ScanVisualbasicFile(path);
//			}
		}
		
	}
}
